package com.wrial.utils;
/*
 * @Author  Wrial
 * @Date Created in 21:00 2020/3/25
 * @Description  Latest Recently used
 */

import java.util.*;

public class LRUCache {



    public static void main(String[] args) {


        LRUCache lruCache = new LRUCache();
        lruCache.addNum(1);
        lruCache.addNum(2);
        lruCache.addNum(3);
        lruCache.addNum(4);
        lruCache.addNum(5);
        lruCache.addNum(1);
        lruCache.addNum(8);

        lruCache.cache.forEach(a->{
            System.out.print(a+" ");
        });
    }


    List<Integer> cache = new LinkedList();
    Set set = new HashSet();
    //1.小于cache容量
    //2.大于cache容量  2.1.找有没有相同  2.2.否则就替换头
    public void addNum(int num) {

        if (cache.size() < 5) {
            cache.add(num);
        } else {
            if (set.contains(num)){
              cache.remove(num);
                set.add(num);
            }else {
                cache.remove(0);
                cache.add(num);
            }
        }
    }

}
